using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace GoogleNet
{
    /// <summary>
    /// Specifies how the description balloon for placemarks is drawn. 
    /// The bgColor, if specified, is used as the background color of the balloon. 
    /// See Feature for a diagram illustrating how the default description balloon 
    /// appears in Google Earth. 
    /// </summary>
    public class KmlBaloonStyle : KmlObject, IKmlElement
    {
        private string tagName = "BaloonStyle";


        #region BgColor
        private Color bgColor;

        /// <summary>
        /// <para>
        /// Background color of the balloon (optional). Color and opacity (alpha) values are expressed 
        /// in hexadecimal notation. The range of values for any one color is 0 to 255 (00 to ff). 
        /// The order of expression is aabbggrr, where aa=alpha (00 to ff); bb=blue (00 to ff); 
        /// gg=green (00 to ff); rr=red (00 to ff). For alpha, 00 is fully transparent and ff is fully opaque. 
        /// </para>
        /// <para>
        /// For example, if you want to apply a blue color with 50 percent opacity to an overlay, 
        /// you would specify the following: &lt;bgColor&gt;7fff0000&lt;/bgColor&gt;, where alpha=0x7f, blue=0xff, green=0x00, and red=0x00. 
        /// </para> 
        /// </summary>
        /// <value>The color.</value>
        public Color BgColor
        {
            get { return bgColor; }
            set { bgColor = value; }
        }
        #endregion

        #region TextColor
        private Color textColor;

        /// <summary>
        /// Foreground color for text. The default is black (00000000). 
        /// </summary>
        /// <value>The color.</value>
        public Color TextColor
        {
            get { return textColor; }
            set { textColor = value; }
        }
        #endregion

        #region Text
        private string text;

        /// <summary>
        /// <para>
        /// Text displayed in the balloon. If no text is specified, Google Earth supplies one. 
        /// You can add entities to the text tag using the following format to refer to a 
        /// child element of Feature: $[name], $[description], $[address], $[id], $[Snippet]. 
        /// Google Earth looks in the current Feature for the corresponding string entity and 
        /// substitutes that information in the balloon. To include To here - From here driving 
        /// directions in the balloon, use the $[geDirections] tag.
        /// </para>
        /// <example>
        /// For example, in the following KML excerpt, $[name] and $[description] fields will be 
        /// replaced by the &lt;name&gt; and &lt;description&gt; fields found in the Feature elements that use this BalloonStyle: 
        /// 
        ///     <code>&lt;text&gt;This is $[name], whose description is:&lt;br&gt;$[description]&lt;/text&gt;  </code>
        /// </example>
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get { return text; }
            set { text = value; }
        } 
        #endregion

        #region IKmlElement Members


        /// <summary>
        /// Name of the KML tag for this specific element.
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        public string TagName
        {
            get { return tagName; }
        }

        #endregion

        #region IKmlGenerator Members

        /// <summary>
        /// Generates the KML for this element including its child elements.
        /// </summary>
        /// <param name="writer">The XML writer that will be used to generate the KML.</param>
        public void GenerateKml(System.Xml.XmlTextWriter writer)
        {
            writer.WriteStartElement(tagName);

            if (bgColor != null)
                writer.WriteElementString("bgColor", convertColorToKmlColor(bgColor));

            if (textColor != null)
                writer.WriteElementString("textColor", convertColorToKmlColor(textColor));

            writer.WriteElementString("text", text);

            writer.WriteEndElement();
        }

        #endregion

        /// <summary>
        /// Converts the .NET color to its KML's hexadecimal representation.
        /// </summary>
        /// <param name="c">The color to convert</param>
        /// <returns></returns>
        private string convertColorToKmlColor(Color c)
        {
            string hexColor = string.Format("{0:x2}", c.A) +
                                string.Format("{0:x2}", c.B) +
                                string.Format("{0:x2}", c.G) +
                                string.Format("{0:x2}", c.R);

            return hexColor;
        }
    }
}
